package com.filldream.fastboot.base.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.filldream.fastboot.base.response.pc.database.GenTableColumnDTO;
import com.filldream.fastboot.common.util.JsonUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

@Data
public class GenTable {

    @TableId(value = "id", type = IdType.AUTO)
    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "表名称")
    private String tableName;

    @ApiModelProperty(value = "表描述")
    private String tableComment;

    @ApiModelProperty(value = "实体类名称")
    private String className;

    @ApiModelProperty(value = "使用的模板（crud单表操作 tree树表操作 sub主子表操作）")
    private String tplCategory;

    @ApiModelProperty(value = "生成包路径,例如：com.filldream.fastboot")
    private String packageName;

    @ApiModelProperty(value = "生成功能")
    private String functionName;

    @ApiModelProperty(value = "生成作者")
    private String functionAuthor;

    @ApiModelProperty(value = "生成代码方式（0zip压缩包 1自定义路径）")
    private String genType;

    @ApiModelProperty(value = "字段配置JSON")
    private String columnJson = "[]";

    @ApiModelProperty(value = "前端目录名")
    private String vuePath ;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "创建者")
    private String createBy;

    @ApiModelProperty(value = "更新者")
    private String updateBy;

    @ApiModelProperty(value = "项目前缀，例如：fastboot-core  fastboot-base 中的 fastboot")
    private String projectPrefix;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @ApiModelProperty(value = "是否覆盖生成")
    private Boolean coverFlag = false;

    @TableField(exist = false)
    private List<GenTableColumnDTO> columnList;

    public void initColumnList(){
        if(StringUtils.isNotBlank(this.getColumnJson())){
            this.columnList = JsonUtil.fromJosnArray(this.getColumnJson(), GenTableColumnDTO.class);
        }
    }

    public void initColumnJson(List<GenTableColumnDTO> list){
        if(ObjectUtils.isNotEmpty( list )){
            this.columnJson  =  JsonUtil.toJson(list);
        }else{
            this.columnJson = "[]";
        }
    }

    public void initColumnJson(){
        if(ObjectUtils.isNotEmpty( this.columnList )){
            this.columnJson  =  JsonUtil.toJson(this.columnList);
        }else{
            this.columnJson = "[]";
        }
    }

}
